#include "DirScanner.h"
#include <iostream>
#include <cstring>
#include <dirent.h>

DirScanner::DirScanner(){
    
}

DirScanner::~DirScanner(){

}

//重载函数调用运算符()，调用traverse函数
void DirScanner::operator()(const string &dirname){
    traverse(dirname);
}

//返回_files的引用
vector<string> &DirScanner::files(){;
    return _files;
}

//获取某一目录下的所有文件名
void DirScanner::traverse(const string &dirname){
    DIR *dir = opendir(dirname.c_str());
    if(dir == NULL){
        cerr << "无法打开目录: " << dir << "\n";
        return;
    }
    struct dirent *entry;
    while((entry = readdir(dir)) != NULL){
        if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0){
            continue;
        }
        _files.push_back(dirname + "/" + string(entry->d_name));
    }
}

//打印所有的文件名
void DirScanner::print(){
    for(auto &i: _files){
        cout << i << endl;
    }
}
